package com.ruyuan.eshop.buyer.controller.trade;

import com.ruyuan.eshop.buyer.domain.request.AddCartRequest;
import com.ruyuan.eshop.buyer.domain.request.CartBuyRequest;
import com.ruyuan.eshop.buyer.domain.request.CartUpdateRequest;
import com.ruyuan.eshop.framework.exception.ServiceException;
import com.ruyuan.eshop.common.enums.TradeErrorCode;
import com.ruyuan.eshop.common.enums.CheckedWay;
import com.ruyuan.eshop.common.domain.vo.CartSkuOriginVo;
import com.ruyuan.eshop.common.domain.vo.CartView;
import com.ruyuan.eshop.common.domain.vo.PriceDetailVO;
import com.ruyuan.eshop.common.mananger.CartOriginDataManager;
import com.ruyuan.eshop.common.mananger.CartReadManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;

/**
 * 购物车接口
 *
 * @author pwd
 * @version 1.0
 */
@Slf4j
@Api(description = "购物车接口模块")
@RestController
@RequestMapping("/api/v1.0/trade/carts")
@Validated
public class CartBuyerController {

    @Autowired
    private CartReadManager cartReadManager;


    @Autowired
    private CartOriginDataManager cartOriginDataManager;

    /**
     * 向购物车中添加一个产品
     * @param addCartRequest
     * @return
     */
    @ApiOperation("向购物车中添加一个产品")
    @PostMapping
    public CartSkuOriginVo add(@RequestBody AddCartRequest addCartRequest) {
        Long skuId = addCartRequest.getSkuId();
        int num = addCartRequest.getNum();
        Long activityId = addCartRequest.getActivityId();
        String promotionType = addCartRequest.getPromotionType();
        return cartOriginDataManager.add(skuId, num, activityId, promotionType);
    }

    /**
     * 立即购买
     * @param cartBuyRequest
     */
    @ApiOperation(value = "立即购买")
    @PostMapping("/buy")
    public void buy(@RequestBody CartBuyRequest cartBuyRequest) {
        cartOriginDataManager.buy(cartBuyRequest.getSkuId(),
                cartBuyRequest.getNum(),
                cartBuyRequest.getActivityId(),
                cartBuyRequest.getPromotionType());
     }

    /**
     * 获取购物车页面购物车详情
     * @param way
     * @return
     */
    @ApiOperation(value = "获取购物车页面购物车详情")
    @GetMapping("/all")
    public CartView cartAll(String way) {
        try{
            return this.cartReadManager.getCartListAndCountPrice(CheckedWay.valueOf(way));
        }catch (Exception e){
            log.error("读取购物车异常",e);
            return new CartView(new ArrayList<>(),new PriceDetailVO());
        }
    }

    /**
     * 获取结算页面购物车详情
     * @param way
     * @return
     */
    @ApiOperation(value = "获取结算页面购物车详情")
    @GetMapping("/checked")
    public CartView cartChecked(String way) {
        try{
            // 读取选中的列表
           return  this.cartReadManager.getCheckedItems(CheckedWay.valueOf(way));
        }catch (Exception e){
            log.error("读取结算页的购物车异常",e);
            return new CartView(new ArrayList<>(),new PriceDetailVO());
        }
    }

    /**
     * 更新购物车中的多个产品
     * @param skuId
     * @param cartUpdateRequest
     */
    @ApiOperation(value = "更新购物车中的多个产品", notes = "更新购物车中的多个产品的数量或选中状态")
    @PostMapping(value = "/sku/{sku_id}")
    public void update(@PathVariable(name = "sku_id") Long skuId, @RequestBody CartUpdateRequest cartUpdateRequest) {
        if (cartUpdateRequest.getChecked() != null) {
            cartOriginDataManager.checked(skuId, cartUpdateRequest.getChecked());
        } else if (cartUpdateRequest.getNum() != null) {
            cartOriginDataManager.updateNum(skuId, cartUpdateRequest.getNum());
        }
    }

    /**
     * 设置全部商为选中或不选中
     * @param cartUpdateRequest
     */
    @ApiOperation(value = "设置全部商为选中或不选中")
    @PostMapping(value = "/checked")
    public void updateAll(@RequestBody CartUpdateRequest cartUpdateRequest) {
        if (cartUpdateRequest.getChecked() != null) {
           cartOriginDataManager.checkedAll(cartUpdateRequest.getChecked(), CheckedWay.CART);
        }
    }

    /**
     * 批量设置某商家的商品为选中或不选中
     * @param sellerId
     * @param cartUpdateRequest
     */
    @ApiOperation(value = "批量设置某商家的商品为选中或不选中")
    @PostMapping(value = "/seller/{seller_id}")
    public void updateSellerAll(@PathVariable(name = "seller_id") Long sellerId,
                                @RequestBody CartUpdateRequest cartUpdateRequest) {
        if (cartUpdateRequest.getChecked() != null && sellerId != null) {
            cartOriginDataManager.checkedSeller(sellerId, cartUpdateRequest.getChecked());
        }
    }

    /**
     * 清空购物车
     */
    @ApiOperation(value = "清空购物车")
    @DeleteMapping
    public void clean() {
        cartOriginDataManager.clean();
    }

    /**
     * 删除购物车中的一个或多个产品
     * @param skuIds
     */
    @ApiOperation(value = "删除购物车中的一个或多个产品")
    @DeleteMapping(value = "/{sku_ids}/sku")
    public void delete(@PathVariable(name = "sku_ids") Long[] skuIds) {
        if (skuIds.length == 0) {
            throw new ServiceException(TradeErrorCode.E455.code(), "参数异常");
        }
        cartOriginDataManager.delete(skuIds,CheckedWay.CART);
    }

}
